package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.Friend;
import com.ruoyi.service.FriendService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Resource
    private FriendService friendService;

    @Resource
    private SysUserMapper sysUserMapper;

    @PostMapping("/add/{id}")
    public AjaxResult add(@PathVariable(value = "id")Long id){
        Friend one = friendService.getOne(new LambdaQueryWrapper<Friend>()
                .eq(Friend::getLoginUserId, SecurityUtils.getUserId())
                .eq(Friend::getLoginUserFriendId, id)
                .last("limit 1")
        );
        if (Objects.nonNull(one)){
            return AjaxResult.error("你们已经是好友了请勿重复添加");
        }


        Friend friend = new Friend();
        friend.setLoginUserId(SecurityUtils.getUserId());
        friend.setLoginUserFriendId(id);
        friendService.save(friend);
        return AjaxResult.success("添加成功");
    }

    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable(value = "id")Long id){
        friendService.remove(new LambdaQueryWrapper<Friend>()
                .eq(Friend::getId,id)
        );
        //返回结果
        return AjaxResult.success("删除成功");
    }

    @PostMapping("/page")
    public AjaxResult getUserFriend(@RequestBody Page<Friend> page,@RequestBody Friend friend){

        List<Long> collet = new ArrayList<>();

        SysUser sysUser = new SysUser();

        if (StringUtils.isNotBlank(friend.getNickName())){
            sysUser.setNickName(friend.getNickName());
            collet = sysUserMapper.selectUserList(sysUser).stream().map(ele->ele.getUserId()).collect(Collectors.toList());
            if (Objects.isNull(collet) || collet.size() == 0){
                return AjaxResult.success(page);
            }
        }

        friendService.page(page,new LambdaQueryWrapper<Friend>()
                .eq(Friend::getLoginUserId, SecurityUtils.getUserId())
                .in(Objects.nonNull(collet) && collet.size() > 0,Friend::getLoginUserFriendId,collet)
        );
        page.setTotal(friendService.count(new LambdaQueryWrapper<Friend>()
                .eq(Friend::getLoginUserId, SecurityUtils.getUserId())
                .in(Objects.nonNull(collet) && collet.size() > 0,Friend::getLoginUserFriendId,collet)
        ));

        page.getRecords().forEach(ele->{
            SysUser friendUser = sysUserMapper.selectUserById(ele.getLoginUserFriendId());
            if (Objects.nonNull(friendUser)) ele.setFriendUser(friendUser);
        });
        //返回结果
        return AjaxResult.success(page);
    }



}
